# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""

"""
#import time

#from Common.publicFunc import update_progress
#from public import *

import sqlite3


# def sql_chaxun(sql_query,mode,db="netzdgd"):
#     #本程序一共使用了2个数据库，分别对应netzdgd和netsj 库。
#     #如果是批量查询，则对应netzdgd数据库。如果是查询套餐，则对应query_p方法和对应写死的netsj表。
#     try:
#         if mode == "pquery":
#             engine = create_engine(f'sqlite:///.\{db}.db')
#             # 使用pandas的read_sql_query函数执行SQL语句，并存入DataFrame
#             df_read = pd.read_sql_query(sql_query, engine)
#             print(df_read)
#             # return df_read
#             # df_read
#             list1 = df_read['jieruhao'].tolist()
#             # list1 = df_read.iloc[0].tolist()
#             print(list1)
#             print(len(list1), '\n')
#             return len(list1), list1
#             # var_strint = ""
#             # for i in range(len(list1)):
#             #     print(list1[i])
#             #     var_strint += str(list1[i])+","
#             # print(var_strint)
#         elif mode == "query_p":
#             '''对应写死的netsj表单个查询'''
#             #pd.read_sql_query("insert into cjk_test h values %(data)s", params={'data': v_split[11]}, con=engine)
#             # pd.read_sql_query(sql_query, engine).head(20)
#             # print("insert is over")
#             sql = simpleToolSql("netsj")
#             res = sql.query(sql_query)
#             print(res)
#             print(type(res))
#             print(" query over")
#             sql.close()
#             return len(res),res
#         elif mode == "query":
#             '''插入操作'''
#             #pd.read_sql_query("insert into cjk_test h values %(data)s", params={'data': v_split[11]}, con=engine)
#             # pd.read_sql_query(sql_query, engine).head(20)
#             # print("insert is over")
#             sql = simpleToolSql(db)
#             res = sql.query(sql_query)
#             print(res)
#             print(type(res))
#             print(" query over")
#             sql.close()
#             return len(res),res
#         elif mode == "insert":
#             '''插入操作'''
#             #pd.read_sql_query("insert into cjk_test h values %(data)s", params={'data': v_split[11]}, con=engine)
#             # pd.read_sql_query(sql_query, engine).head(20)
#             # print("insert is over")
#             # pass
#             sql = simpleToolSql(db)
#             sql.execute(sql_query)
#             print(" insert over")
#             sql.close()
#
#         elif mode == "update":
#             '''插入操作'''
#             # pd.read_sql(sql_query, engine)
#             # print("update is over")
#             # pass
#
#             # f = sql.execute("create table test (id int not null,name text not null,age int);")
#             # print("ok")
#             # sql.execute("insert into test (id,name,age) values (?,?,?);", [(1, 'abc', 15), (2, 'bca', 16)])
#             # res = sql.query("select * from test;")
#             # print(res)
#             #res = sql.execute(sql_query)
#             sql = simpleToolSql(db)
#             sql.execute(sql_query)
#             #res = sql.query("select * from test where id=?;", (3,))
#             print(" update over")
#             sql.close()
#
#         else:
#             return "无动作！"
#     except:
#         print("获取失败，继续下一个循环。")


#第二部分营销反馈部分内容开始
class simpleToolSql():
    """
    simpleToolSql for sqlite3
    简单数据库工具类
    编写这个类主要是为了封装sqlite，继承此类复用方法
    """

    def __init__(self, filename="netsj"):
        """
        初始化数据库，默认文件名 stsql.db
        filename：文件名
        """
        self.filename = filename + ".db"
        self.db = sqlite3.connect(self.filename)
        self.c = self.db.cursor()

    def close(self):
        """
        关闭数据库
        """
        self.c.close()
        self.db.close()

    def execute(self, sql, param=None):
        """
        执行数据库的增、删、改
        sql：sql语句
        param：数据，可以是list或tuple，亦可是None
        retutn：成功返回True
        """
        try:
            if param is None:
                self.c.execute(sql)
            else:
                if type(param) is list:
                    self.c.executemany(sql, param)
                else:
                    self.c.execute(sql, param)
            count = self.db.total_changes
            self.db.commit()
        except Exception as e:
            print(e)
            return False, e
        if count > 0:
            return True
        else:
            return False

    def query(self, sql, param=None):
        """
        查询语句
        sql：sql语句
        param：参数,可为None
        retutn：成功返回True
        """
        if param is None:
            self.c.execute(sql)
        else:
            self.c.execute(sql, param)
        return self.c.fetchall()

    # def set(self,table,field=" * ",where="",isWhere=False):
    #     self.table = table
    #     self.filed = field
    #     if where != "" :
    #         self.where = where
    #         self.isWhere = True
    #     return True
#第二部分